package GUI;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Vector2f;

import Game.GameHelper;
import Game.Items.GameItem;
import Game.Objects.GOPlayer;
import Game.Objects.GOSeller;
import Tools.SoundManager;
import Tools.Drawing.FontTagSystem;

/** GUI Seller class
 * @author Aikidos
 */
public class GuiSeller {
	
	/** GUI is open? */
	private boolean _isOpen;
	
	/** Player */
	private GOPlayer _player;
	
	/** Seller */
	private GOSeller _seller;
	
	/** Icon 'buy' white */
	private Image _imageBuyWhite;
	
	/** Icon 'buy' red */
	private Image _imageBuyRed;
	
	/** Selected index */
	private int _selected;
	
	/** Maximum selected index */
	private int _selectedMax;
	
	public GuiSeller(GOPlayer player) throws SlickException {
		_player = player;
		
		_imageBuyWhite = new Image("res/gfx/icons/buy_white.png");
		_imageBuyRed = new Image("res/gfx/icons/buy_red.png");
	}
	
	/** Returns true if GUI is open
	 * @return True if GUI is open
	 */
	public boolean isOpen() {
		return _isOpen;
	}
	
	/** Open GUI */
	public void open(GOSeller seller) {
		_seller = seller;
		_selected = 0;
		_selectedMax = seller.getItems().size() - 1;
		
		_isOpen = true;
		
		// Lock camera control
		_player.getObjectManager().getCamera().lock();
	}
	
	/** Close GUI */
	public void close() {
		_isOpen = false;
	}
	
	public void update(int delta) {
		if (_isOpen) {
			// Get input system
			Input input = GameHelper.getGame().getInput();
			
			// Exit
			if (input.isKeyPressed(Input.KEY_W))
				close();
			if (input.isKeyPressed(Input.KEY_A))
				close();
			if (input.isKeyPressed(Input.KEY_S))
				close();
			if (input.isKeyPressed(Input.KEY_D))
				close();
			if (input.isKeyPressed(Input.KEY_E))
				close();
			
			// Select
			if (input.isKeyPressed(Input.KEY_DOWN)) {
				_selected++;
				if (_selected > _selectedMax)
					_selected = _selectedMax;
				
				// Play sound
				SoundManager.play("sfx_skill");
			}

			if (input.isKeyPressed(Input.KEY_UP)) {
				_selected--;
				if (_selected < 0)
					_selected = 0;
				
				// Play sound
				SoundManager.play("sfx_skill");
			}
			
			// Buy
			if (input.isKeyPressed(Input.KEY_RETURN)) {
				GameItem item = _seller.getItems().get(_selected);
				if (_player.Gold >= item.getCost()) {
					_player.Gold -= item.getCost();
					_player.getInventory().addItem(item);
					_seller.getItems().remove(_selected);
					_selectedMax = _seller.getItems().size() - 1;
					if (_selected > _selectedMax) _selected--;
					
					// Sound
					SoundManager.play("sfx_buy");
				}
			}
		}
	}
	
	public void draw(Graphics g) {
		if (_isOpen) {
			g.setColor(Color.black);
			g.fillRect(0, 100, GameHelper.getGame().getWidth(), 400);
			
			g.setColor(Color.white);
			g.drawLine(0, 99, GameHelper.getGame().getWidth(), 99);
			g.drawLine(0, 502, GameHelper.getGame().getWidth(), 502);
			
			//////////////////////////
			////////// Draw //////////
			//////////////////////////
			
			// Dialog
			if (_seller.getItems().size() > 0) {
				FontTagSystem.drawText("[green]Tradesman:[/green] Hello, stranger!", new Vector2f(20, 120));
			} else {
				FontTagSystem.drawText("[green]Tradesman:[/green] I have no items :(", new Vector2f(20, 120));
			}
			
			// Gold
			if (_player.Gold > 0)
				FontTagSystem.drawText("Gold: [green]" + _player.Gold + "g[/green]", 
						new Vector2f(GameHelper.getGame().getWidth() - 20, 470), FontTagSystem.EAlign.RIGHT);
			else
				FontTagSystem.drawText("Gold: [red]0g[/red]", 
						new Vector2f(GameHelper.getGame().getWidth() - 20, 470), FontTagSystem.EAlign.RIGHT);
			
			// Items
			Vector2f drawPos = new Vector2f(20, 170);
			
			for (GameItem gameItem : _seller.getItems()) {
				FontTagSystem.drawText(gameItem.getName() + " - [yellow]" + String.valueOf(gameItem.getCost()) + "g[/yellow]", drawPos);
				drawPos.y += 30;
			}
			
			/////////////////////////////////
			////////// Description //////////
			/////////////////////////////////
			if (_seller.getItems().size() > 0) {
				GameItem selectedItem = _seller.getItems().get(_selected);
				selectedItem.drawInfo(new Vector2f(350, 120), g);
			}
			
			// Buy icon
			if (_seller.getItems().size() > 0) {
				GameItem selectedItem = _seller.getItems().get(_selected);
				if (_player.Gold >= selectedItem.getCost()) {
					drawPos = new Vector2f(230, 173 + (_selected * 30));
					_imageBuyWhite.draw(drawPos.x, drawPos.y);
				} else {
					drawPos = new Vector2f(230, 173 + (_selected * 30));
					_imageBuyRed.draw(drawPos.x, drawPos.y);
				}
			}
		}
	}
	
}
